package www.bplead.com.util;

import java.nio.charset.Charset;
import java.util.List;

/**
 * Created by wangyangzi on 2017/3/3.
 */

public class PrintFormatUtil {
    public static class TableType{
        public static final int THREE_COL = 3;
        public static final int FOUR_COL = 4;
    }

    public static String printTitle(String title) {
        int LINE_BYTE_SIZE = 49;
        StringBuffer sb = new StringBuffer();
        //获取数据长度
        int dataLength = title.getBytes(Charset.forName("GB2312")).length;
        //获取空格,居中
        for (int i = 0; i < (LINE_BYTE_SIZE - getBytesLength(title)) / 2; i++) {
            sb.append(" ");
        }
        sb.append(title);
        return sb.toString();
    }

    public static int getBytesLength(String msg) {
        return msg.getBytes(Charset.forName("GB2312")).length;
    }

    public static String printHeader(String[] header, int tableType) {
        StringBuffer sb = new StringBuffer();
        //三列表格
        if (tableType == TableType.THREE_COL) {
            String t1 = header[0] + getSpaceStr(1, 10 - header[0].length());
            String t2 = header[1] + getSpaceStr(1, 10 - header[1].length());
            String t3 = header[2] + getSpaceStr(1, 4 - header[2].length());
            sb.append(t1);
            sb.append(t2);
            sb.append(t3);
        }
        //四列表格
        else if(tableType == TableType.FOUR_COL){
            String t1 = header[0] + getSpaceStr(1, 8 - header[0].length());
            String t2 = header[1] + getSpaceStr(1, 6 - header[1].length());
            String t3 = header[2] + getSpaceStr(1, 6 - header[2].length());
            String t4 = header[3] + getSpaceStr(1, 4 - header[3].length());
            sb.append(t1);
            sb.append(t2);
            sb.append(t3);
            sb.append(t4);
        }
        //换行
        sb.append("\n");

        return sb.toString();

    }

    /**
     * 打印四列
     *
     * @param colList
     * @return
     */
    public static String print4Col(List<String[]> colList) {
        int col1Length = 8;//类别(占8个全角字符)
        int col2Length = 12;//规格(占12个半角字符)
        int col3Length = 6;//级别(占6个全角字符)
        int col4Length = 4;//数量(占4个半角字符)

        //24全角字符 = col1Length + col2Length + col3Length + col4Length

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < colList.size(); i++) {
            String[] colStr = colList.get(i);
            //类别 - 全角
            int spaceCount = col1Length - colStr[0].length();
            String col1String = colStr[0] + getSpaceStr(1, spaceCount);
            sb.append(col1String);
            //规格 - 半角
            spaceCount = col2Length - colStr[1].length();
            String col2String = colStr[1] + getSpaceStr(2, spaceCount);
            sb.append(col2String);
            //级别 - 全角
            spaceCount = col3Length - colStr[2].length();
            String col3String = colStr[2] + getSpaceStr(1, spaceCount);
            sb.append(col3String);
            //数量 - 全角
            spaceCount = col4Length - colStr[3].length();
            String col4String = colStr[3] + getSpaceStr(2, spaceCount);
            sb.append(col4String);
            //换行
            sb.append("\n");
        }

        return sb.toString();
    }

    /**
     * 打印三列
     *
     * @param colList
     * @return
     */
    public static String print3Col(List<String[]> colList) {
        int col1Length = 10;//类别(占10个全角字符)
        int col2Length = 20;//规格(占20个半角字符)
        int col3Length = 8;//级别(占8个全角字符)

        //24全角字符 = col1Length + col2Length + col3Length

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < colList.size(); i++) {
            String[] colStr = colList.get(i);
            //类别 - 全角
            int spaceCount = col1Length - colStr[0].length();
            String col1String = colStr[0] + getSpaceStr(1, spaceCount);
            sb.append(col1String);
            //规格 - 半角
            spaceCount = col2Length - colStr[1].length();
            String col2String = colStr[1] + getSpaceStr(2, spaceCount);
            sb.append(col2String);
            //级别 - 半角
            spaceCount = col3Length - colStr[2].length();
            String col3String = colStr[2] + getSpaceStr(2, spaceCount);
            sb.append(col3String);
            //换行
            sb.append("\n");
        }

        return sb.toString();
    }

    public static String getSpaceStr(int type, int length) {
        String result = "";
        for (int i = 0; i < length; i++) {
            if (type == 1) {//全角
                result += "　";
            } else {//半角
                result += " ";
            }
        }
        return result;
    }
}
